package edu.faytechcc.csc.gui.server;

import edu.faytechcc.csc.messages.NetworkConnection;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 * Creates a class to be used as a multi server for communication between a
 * client and the database.
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class MultiSearchServer
{

    /**
     *
     * @param args
     */
    public static void main(String[] args)
    {

        ServerSocket serverSocket = null;
        // Creates an array of NetworkConnections so that it can be a
        // multiconnection server
        ArrayList<NetworkConnection> connections = new ArrayList<>();

        // Gets the socket to use for connections
        try
        {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e)
        {
            System.out.println("Could not listen on port 4444");
            System.exit(1);
        }

        Socket clientSocket = null;
        try
        {
            boolean connect = true;
            while (connect)
            {

                System.out.println("Waiting for client connection...");
                clientSocket = serverSocket.accept();
                System.out.println("Client connected: "
                        + clientSocket.getInetAddress());
                System.out.println("+++++++++++++\n");

                NetworkConnection conn = new NetworkConnection(clientSocket);
                conn.connectServer();
                connections.add(conn);
                AskQuery general = new AskQuery(conn);
                conn.addServer(general);
            }
            serverSocket.close();

        } catch (IOException e)
        {
            JOptionPane.showMessageDialog(null, "Client Accept failded");
        }
    }
}
